package com.ysh.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.ysh.common.Code;
import com.ysh.common.Result;
import com.ysh.common.ResultFactory;
import com.ysh.exception.BusinessException;
import com.ysh.mapper.UserMapper;
import com.ysh.pojo.User;
import com.ysh.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * 用户业务层接口实现类
 */
@Service  // 业务层交给spring容器管理
@Transactional  // 开启事务支持
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;
    /**
     * 新增用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean save(User user) {
        return userMapper.save(user);
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean update(User user) {
        return userMapper.update(user);
    }

    /**
     * 删除用户
     *
     * @param uuid
     * @return
     */
    @Override
    public boolean delete(Integer uuid) {
        return userMapper.delete(uuid);
    }

    /**
     * 查询单个用户信息
     *
     * @param uuid
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public User get(Integer uuid) {
        return userMapper.get(uuid);
    }

    /**
     * 分页查询
     * @param page 当前页，从1开始
     * @param size 每页显示记录数
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public Page<User> getAll(Integer page,Integer size) {

        // 报错
        // 其他异常
        // int i = 1/0;
        // Business异常
        // if(true) throw new BusinessException(new Result(Code.ERROR,"建议更换电脑重试！"));


        // 如果参数为空
        PageHelper.startPage(page, size);
        Page<User> pageList = (Page<User>) userMapper.getAll();
        return pageList;
    }

    /**
     * 模糊查询
     *
     * @param user 条件，封装成对象
     * @param page 当前页
     * @param size 每页条数
     * @return
     */
    @Override
    public Page<User> getUsers(User user, Integer page, Integer size) {

        PageHelper.startPage(page, size);
        Page<User> pageList = (Page<User>) userMapper.getUsers(user);
        return pageList;
    }

    /**
     * 根据用户名和密码登录
     *
     * @param userName 用户名
     * @param password 密码
     * @return
     */
    @Transactional(readOnly = true)
    @Override
    public User login(String userName, String password) {
        if (StringUtils.isEmpty(userName)){
            throw new BusinessException(ResultFactory.ERROR("用户名不能为空！"));
        }
        if (StringUtils.isEmpty(password)){
            throw new BusinessException(ResultFactory.ERROR("密码不能为空！"));
        }

        User user = userMapper.getByUserNameAndPassword(userName,password);
        if (user == null){
            throw new BusinessException(ResultFactory.ERROR("用户名或密码错误！"));
        }
        return user;
    }
}
